package com.zyb.insurance.policy.entity;

import com.zyb.insurance.policy.constant.*;

import java.util.*;

public class InsurancePolicyFactory {

    public static InsurancePolicy build() {
        InsurancePolicy insurancePolicy = new InsurancePolicy();
        insurancePolicy.setInsurancePolicyId(UUID.randomUUID().toString().replace("-", ""));
        insurancePolicy.setTypeMode(TypeMode.COMMON_INSURANCE_POLICY);
        insurancePolicy.setInsuranceContract(buildInsuranceContract());
        insurancePolicy.setPolicyHolder(buildPolicyHolder());
        insurancePolicy.setInsurancePolicyContractStatus(buildInsurancePolicyContractStatus());
        insurancePolicy.setInsurancePlan(buildInsurancePlan());
        insurancePolicy.setVideoMaterials(buildVideoMaterials());
        insurancePolicy.setChangeHistories(buildChangeHistories());

        return insurancePolicy;
    }

    private static InsuranceContract buildInsuranceContract() {
        InsuranceContract insuranceContract = new InsuranceContract();
        insuranceContract.setIssueInsurancePolicyOrganization(buildInsuranceShop());
        insuranceContract.setIssueInsurancePolicyStaff(buildEmployee());
        insuranceContract.setOperator("经办人" + getRandomString());
        insuranceContract.setInsuranceCompany(buildInsuranceCompany());
        insuranceContract.setInsuranceProduct(buildInsuranceProduct());
        insuranceContract.setContractCode("合同号" + getRandomString());
        insuranceContract.setRelatedContractCode("关联合同号" + getRandomString());
        insuranceContract.setProposalFormCode("投保单号" + getRandomString());
        insuranceContract.setPayType(PayType.YEAR_PAY);
        insuranceContract.setSaleType(SaleType.APP);
        insuranceContract.setPayPeriod(1);
        insuranceContract.setInsurancePolicyYearNumber(1);
        insuranceContract.setRemarkContent("备注内容" + getRandomString());

        List<String> tags = new ArrayList<String>();
        tags.add("标签" + getRandomString());
        tags.add("标签" + getRandomString());
        insuranceContract.setTags(tags);

        List<InsurancePolicyAttendant> attendants =
                new ArrayList<InsurancePolicyAttendant>();
        attendants.add(new InsurancePolicyAttendant(buildEmployee(), getRandomPastDayDate(), getRandomFutureYearDate()));
        attendants.add(new InsurancePolicyAttendant(buildEmployee(), getRandomPastDayDate(), getRandomFutureYearDate()));
        insuranceContract.setInsurancePolicyAttendants(attendants);

        return insuranceContract;
    }

    private static InsuranceProduct buildInsuranceProduct() {
        InsuranceProduct insuranceProduct = new InsuranceProduct();
        insuranceProduct.setInsuranceProductType("保险产品类型" + getRandomString());
        insuranceProduct.setName("保险产品名称" + getRandomString());
        insuranceProduct.setNamePhoneticAbbreviate("保险产品品名缩写" + getRandomString());
        insuranceProduct.setSaling(true);
        return insuranceProduct;
    }

    private static InsuranceShop buildInsuranceShop() {
        InsuranceShop insuranceShop = new InsuranceShop();
        insuranceShop.setHigherLevelInsuranceShop(null);
        insuranceShop.setInsuranceShopCode("保险门店编号" + getRandomString());
        insuranceShop.setInsuranceShopName("保险门店" + getRandomString());
        insuranceShop.setIsOperating(true);
        insuranceShop.setLowerLevelInsuranceShops(null);
        return insuranceShop;
    }

    private static Employee buildEmployee() {
        Employee employee = new Employee();
        employee.setChineseName("员工姓名" + getRandomString());
        employee.setChineseNamePhoneticAbbreviate("姓名拼音" + getRandomString());
        employee.setEmployeeCode("员工编号" + getRandomString());
        employee.setOnTheJob(true);
        return employee;
    }

    private static InsuranceCompany buildInsuranceCompany() {
        InsuranceCompany insuranceCompany = new InsuranceCompany();
        insuranceCompany.setChineseName("公司名称" + getRandomString());
        insuranceCompany.setChineseNameAbbreviate("名称缩写" + getRandomString());
        insuranceCompany.setChineseNamePhoneticAbbreviate("名称拼音" + getRandomString());
        insuranceCompany.setInsuranceCompanyType(InsuranceCompanyType.LIFE_INSURANCE);
        return insuranceCompany;
    }

    private static PolicyHolder buildPolicyHolder() {
        PolicyHolder policyHolder = new PolicyHolder();
        policyHolder.setMainBodyType(PolicyHolderMainBodyType.NATURAL_PERSON);
        policyHolder.setName("投保人姓名" + getRandomString());
        policyHolder.setCredentialsType(PolicyHolderCredentialsType.IDENTITY_CARD);
        policyHolder.setCredentialsNumber("身份证号" + getRandomString());
        policyHolder.setCredentialsExpiryDate(getRandomFutureYearDate());
        policyHolder.setGender(PolicyHolderGender.MALE);
        policyHolder.setBirthday(getRandomPastYearDate());
        policyHolder.setAge(30);
        policyHolder.setNationality("中国");
        policyHolder.setNation("民族");
        policyHolder.setMarriageStatus("已婚");
        policyHolder.setEducationalBackground("本科");
        policyHolder.setPhoneNumber("手机号" + getRandomString());
        policyHolder.setAddress("住址" + getRandomString());
        policyHolder.setCompany("工作单位" + getRandomString());
        policyHolder.setPosition("职位" + getRandomString());
        policyHolder.setCareer("职业" + getRandomString());
        policyHolder.setBankCardNumber("银行卡号" + getRandomString());
        policyHolder.setBankName("银行名称" + getRandomString());
        policyHolder.setAccountName("账户名称" + getRandomString());

        return policyHolder;
    }

    private static InsurancePolicyContractStatus buildInsurancePolicyContractStatus() {
        InsurancePolicyContractStatus insurancePolicyContractStatus =
                new InsurancePolicyContractStatus();
        insurancePolicyContractStatus.setDeliveryDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setInsureDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setFirstExaminationDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setHesitatePeriodExpiryDate(getRandomFutureDayDate());
        insurancePolicyContractStatus.setSecondExaminationDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setExpiredDate(getRandomFutureYearDate());
        insurancePolicyContractStatus.setNoticeSignDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setReceiptSignDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setReceiptInputDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setReturnVisitDate(getRandomPastDayDate());
        insurancePolicyContractStatus.setPostponeDate(null);
        insurancePolicyContractStatus.setRejectDate(null);
        insurancePolicyContractStatus.setRefundDate(null);
        insurancePolicyContractStatus.setCancelDate(null);
        insurancePolicyContractStatus.setExpiredDate(null);
        insurancePolicyContractStatus.setResumeDate(null);

        return insurancePolicyContractStatus;
    }

    private static InsurancePlan buildInsurancePlan() {
        Random random = new Random();

        InsurancePlan insurancePlan = new InsurancePlan();
        insurancePlan.setInsuranceTypeCode("险种编码" + getRandomString());
        insurancePlan.setProductSpecification("产品规格" + getRandomString());
        insurancePlan.setUnderlyingAssets("保险标的物" + getRandomString());
        insurancePlan.setInsurancePremium(random.nextDouble());
        insurancePlan.setInsuranceAmount(random.nextDouble());
        insurancePlan.setCommissionIncome(random.nextDouble());
        insurancePlan.setBrokeragePay(random.nextDouble());
        insurancePlan.setAcceptInsuranceDate(getRandomPastDayDate());
        insurancePlan.setBeginValidDate(getRandomPastDayDate());
        insurancePlan.setPayDate(getRandomPastDayDate());
        insurancePlan.setVerifyAccountDate(getRandomPastDayDate());
        insurancePlan.setGiveOutBrokerageDate(getRandomPastDayDate());
        insurancePlan.setSettleBrokerageDate(getRandomFutureDayDate());
        insurancePlan.setReportDate(getRandomPastDayDate());

        return insurancePlan;
    }

    private static VideoMaterials buildVideoMaterials() {
        VideoMaterials videoMaterials = new VideoMaterials();
        videoMaterials.setInsureMaterials("投保资料存储地址" + getRandomString());
        videoMaterials.setDoubleRecordMaterials("双录资料存储地址" + getRandomString());
        videoMaterials.setInsuranceContract("保险合同存储地址" + getRandomString());
        videoMaterials.setReceipt("回执存储地址" + getRandomString());
        videoMaterials.setCancelApplicant("撤单申请书存储地址" + getRandomString());
        videoMaterials.setRefundApplicant("退保申请书存储地址" + getRandomString());
        videoMaterials.setAssureApplicant("保全申请书存储地址" + getRandomString());
        videoMaterials.setCustomerNotice("客户告知书存储地址" + getRandomString());
        videoMaterials.setOther("其他资料存储地址" + getRandomString());
        return videoMaterials;
    }

    private static List<ChangeHistory> buildChangeHistories() {
        List<ChangeHistory> changeHistories = new ArrayList<ChangeHistory>();

        Random random = new Random();
        int number = random.nextInt(20) + 1;

        for (int i = 0; i < number; i++) {
            changeHistories.add(new ChangeHistory(
                    "变更字段" + getRandomString(),
                    "变更前" + getRandomString(),
                    "变更后" + getRandomString(),
                    getRandomPastDayDate(),
                    "操作人" + getRandomString()));
        }

        return changeHistories;
    }

    private static String getRandomString() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    private static Date getRandomPastYearDate() {
        Random random = new Random();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -(random.nextInt(10) + 1));

        return calendar.getTime();
    }

    private static Date getRandomPastDayDate() {
        Random random = new Random();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -(random.nextInt(10) + 1));

        return calendar.getTime();
    }

    private static Date getRandomFutureYearDate() {
        Random random = new Random();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, random.nextInt(10) + 1);

        return calendar.getTime();
    }

    private static Date getRandomFutureDayDate() {
        Random random = new Random();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, random.nextInt(10) + 1);

        return calendar.getTime();
    }

}
